<template>
	<view class="addworkBox">
		<view class="itembox">
			<view class="item">
				<view class="title">
					公司名称
				</view>
				<view class="valbox">
					
					<input type="text" v-model="datainfo.name" placeholder="请输入公司名称" />
				</view>
			</view>
			<view class="item">
				<view class="label">
					在职时间
				</view>
				<view class="value">
					<picker mode="date" :value="datainfo.time1" @change="timeChange($event,'time1')">
						<view class="uni-input">{{time1}}</view>
					</picker>
					<view class="line"></view>
					<picker mode="date" :value="datainfo.time2" @change="timeChange($event,'time2')">
						<view class="uni-input">{{time2}}</view>
					</picker>
				</view>
			</view>
			<view class="item">
				<view class="title">
					工作岗位
				</view>
				<view class="valbox">
					<input type="text" v-model="datainfo.position" placeholder="请输入工作岗位" />
				</view>
			</view>
		</view>
		<view class="add" @click="add">
			<text>
				确定
			</text>

		</view>
	</view>
</template>

<script>
	export default {

		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				datainfo:{
					position: "",
					name: "",
				},
				time1: currentDate,
				time2: currentDate,
			}
		},
		onLoad(option) {
			let tempobj = {
				
			}
			if(option.position != undefined && option.position != null) tempobj.position = option.position
			if(option.name != undefined && option.name != null) tempobj.name = option.name
			if(option.time1 != undefined && option.time1 != null) this.time1 = option.time1
			if(option.time2 != undefined && option.time2 != null) this.time2 = option.time2
			
			this.datainfo = Object.assign({},tempobj)
			
		},
		onShow() {
			this.name = "";
			this.position = "";
		},
		methods: {
			timeChange(e, key) {
				console.log()
				console.log()
				if(e.target.value && new Date(e.target.value).getTime() < new Date().getTime()){
					this[key] = e.target.value
				}
				else{
					uni.showToast({
						title:"结束时间不能大于当前时间",
						icon:"none"
					})
				}
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			add() {
				let that = this
				if(!that.datainfo.position || !that.datainfo.name){
					uni.showToast({
						title:'请填写完整信息',
						icon:'none'
					})
					return false;
				}
				var pages = getCurrentPages();
				var prevPage = pages[pages.length - 2]; //上一个页面
				if (this.datainfo.position != "") {
					prevPage.setData({
						userdata: {
							'time1': that.time1,
							'time2': that.time2,
							'position': that.datainfo.position,
							'name': that.datainfo.name,
						}
					})
				}
				uni.navigateBack()
			}
		},

	}
</script>

<style lang="less">
	.itembox {
		padding: 30rpx;

		.item {
			border-bottom: 1rpx solid #C7BEBE;
			padding: 20rpx 0;

			.value {
				&::after {
					content: "";
					display: block;
					height: 0;
					overflow: hidden;
					clear: both;
				}
			}

			.title {
				font-size: 24rpx;
				color: #333;
				margin-bottom: 20rpx;
			}

			.line {
				width: 88rpx;
				height: 2rpx;
				float: left;
				background-color: #D9D9D9;
				margin: 0 18rpx;
				margin-top: 22rpx;
			}

			picker {
				width: 270rpx;
				height: 46rpx;
				float: left;
				background-color: #fff;
				line-height: 46rpx;
				text-align: center;
				font-size: 30rpx;
			}

			input {
				line-height: 48rpx;
				font-size: 28rpx;
			}
		}
	}

	.add {
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		height: 166rpx;
		padding: 30rpx;
		box-shadow: -1px -2px 7px 0px rgba(0, 0, 0, 0.1);

		text {
			display: block;
			height: 88rpx;
			line-height: 88rpx;
			background-color: #E41805;
			color: #fff;
			text-align: center;
			border-radius: 10rpx;
		}

	}
</style>
